package com.liugan.magic.screen;

import org.loon.framework.android.game.core.EmulatorButtons;
import org.loon.framework.android.game.core.EmulatorListener;
import org.loon.framework.android.game.core.LSystem;
import org.loon.framework.android.game.core.graphics.Screen;
import org.loon.framework.android.game.core.graphics.component.LButton;
import org.loon.framework.android.game.core.graphics.opengl.GLEx;
import org.loon.framework.android.game.core.graphics.opengl.LTexture;
import org.loon.framework.android.game.core.input.LTouch;
import org.loon.framework.android.game.core.timer.LTimerContext;

import android.util.Log;
import android.widget.Toast;

import com.liugan.magic.Constants;
import com.liugan.magic.actor.*;
import com.liugan.magic.map.Magicmap;
import com.liugan.magic.screen.layer.GameLayer;
import com.liugan.magic.sound.ASound;
import com.liugan.magic.utils.AnimationUtils;
import com.liugan.magic.utils.ImagesUtils;
import com.liugan.magic.utils.ImagesUtils4layer;
import com.liugan.magic.utils.ImagesUtils4start;

public class GameScreen extends Screen{
	
	//以贴吧留言为对话制作的游戏
	GameLayer glayer;
	public GameLayer getGlayer(){
		return glayer;
	}
	boolean gamestart;
	boolean gameinfo;
	boolean hasbg;
	//实现碰撞检测.并对多种多样的碰撞进行分类.
	//动画变化.变幻.碰撞. 种类不同的actor 属性不同.碰撞也不同.
	private Magicmap magicmap;
	//游戏关卡地图
	//boolean ismove=true;//是否可以移动物体 比如在变幻时不能移动
	PointXY totalXY=new PointXY();//主角的xy
	PointXY offsetXY=new PointXY();//主角在屏幕中的xy 也是setlocation使用的xy
	PointXY beginXY=new PointXY();//屏幕开始的xy  是 totalXY-offsetXY得到的
	Illusionist magic;
	
	private EmulatorListener emulatorListener;
	
	//actor 的类型有 主角 石头 树木 青蛙 狐狸 蝴蝶 钥匙 这些是在根据map来加载的
	//行动类  标识是否在前进 跳跃    将要使用actionkey类代替了.
	private ActionKey goLeftKey;
	private ActionKey goRightKey;
	private ActionKey upKey;
	private ActionKey downKey;
	private ActionKey jumpKey;
	private ActionKey cancelKey;
	private ActionKey transformerlKey;
	
	///////////=====================start使用start=============================

	private int layertime=0;
	int a_x,a_y,s_x,s_y,i_x,i_y,sound_x,sound_y;
	
	///////////======================start使用end============================
	
	//screen
	public GameScreen(){
		//Toast.makeText(this, Constants.nocomplete, 0);
		
	}
	
	
	
	public void mydestroy(){
		//首先干掉actor
		glayer.issucceed=false;
		for (int i=0;i<glayer.getActorList().size();i++){
			glayer.getActorList().get(i).dispose();
		}
		//干掉动画
		AnimationUtils.getinstance().destroy();
		//干掉背景,替换背景
		ImagesUtils.getinstance().destroy();
		magicmap=null;
		LSystem.gc();
		//初始化 主角和glayer
		magic.rebegin();
		
		//加载新的关卡
		
	}
	
	
	public synchronized void startgame(){
		
		ImagesUtils4start.destroy();
		centerOn(glayer);
		add(glayer);
		magicmap=new Magicmap("actormap.data","imagemap.data",glayer,totalXY,offsetXY);
		glayer.setmap(magicmap);
	}
	
	/** 代替onLoaded 加载GameLayer
	 * 
	 */
	public void onLoaded() {
		

		magic=new Illusionist(totalXY,offsetXY);
		glayer= new GameLayer(totalXY,offsetXY,beginXY,magic);
		
		//

		setKeys();
		loadimages4start();
		ASound.getInstance().s1();
	}
	
	
	private void loadimages4start() {
		
		a_x=Constants.ScreenWIDTH/2-ImagesUtils4start.getinstance().armorgames.getWidth()/2;
		a_y=Constants.ScreenHEIGHT/2-ImagesUtils4start.getinstance().armorgames.getHeight()/2;
		i_x=Constants.ScreenWIDTH/2-ImagesUtils4start.getinstance().info.getWidth()/2;
		i_y=Constants.ScreenHEIGHT/2-ImagesUtils4start.getinstance().info.getHeight()/2;
		s_x=Constants.ScreenWIDTH/2-ImagesUtils4start.getinstance().start.getWidth()/2;
		s_y=Constants.ScreenHEIGHT/2-ImagesUtils4start.getinstance().start.getHeight()/2;
		
	}

	@Override
	public void update(long elapsedTime) {
		super.update(elapsedTime);
	}

	@Override
	public void alter(LTimerContext timer) {
		if(glayer.issucceed){
			mydestroy();
		}
		
		PressedKey();
		//if (layertime%800==0) LSystem.gc();
		if (layertime<=1600) {
			layertime++;
		}else if (layertime==1601&&gamestart){
			layertime=0;
		}
	}
	
	@Override
	public void draw(GLEx g) {
		
		if (gamestart){
			//根据主角的xy坐标 得到偏移值. (偏移值是一个相对坐标 是重新计算的xy) 偏移值将计算到地图.actormap(也就是所有的actor的xy里面)
			//偏移值计算得到的偏移坐标.也就是 最后得到的显示地图的 xy 和对象的xy
			if (totalXY.isinit&&offsetXY.isinit){
				beginXY.setxy(totalXY.x-offsetXY.x, totalXY.y-offsetXY.y);
				int gx=totalXY.x-offsetXY.x;
				int gy=totalXY.y-offsetXY.y;
				
				if (gx>0){
					gx*=0.82;//背景缓慢
				}
				if (magicmap!=null) magicmap.draw(g,gx, gy);
				
			}else{
				if (magicmap!=null) magicmap.draw(g,0, 0);
			}
			
			emulator();
		}else{
			if (emulatorListener != null) {
				emulatorListener=null;
			}
			drawmemu(g);
		}
	}

	private void drawmemu(GLEx g) {
		if (gameinfo&&layertime>110){
			g.drawTexture(ImagesUtils4start.getinstance().info,i_x,i_y );//游戏介绍
			return;
		}
		if (layertime<110){
			g.drawTexture(ImagesUtils4start.getinstance().armorgames,a_x,a_y );//盾牌
		}else {
			
			if (!hasbg){setBackground(ImagesUtils4layer.getinstance().bg);hasbg=true;}
			g.drawTexture(ImagesUtils4start.getinstance().start,s_x,s_y );//初始画面
		}
	}

	@Override
	public void touchDown(LTouch e) {
		if (layertime>110){
			if (gameinfo){
				if (!gamestart&&e.getX()>150&&e.getX()<(Constants.ScreenWIDTH-150)&&e.getY()>100&&e.getY()<(Constants.ScreenHEIGHT-100)){
					gamestart=true;
					ASound.getInstance().getS2().stop();
					
					startgame();
				}
			}else{
				gameinfo=true;
				if (!hasbg){setBackground(ImagesUtils4layer.getinstance().bg);hasbg=true;}
				ASound.getInstance().s2();
				ASound.getInstance().getS1().stop();
			}
		}
		
	}

	@Override
	public void touchMove(LTouch e) {
		
	}

	@Override
	public void touchUp(LTouch e) {
		
	}

	
	private void emulator(){
		
		if (emulatorListener == null) {
			emulatorListener = new EmulatorListener() {
				public void onCancelClick() {
					cancelKey.press();
				}
				public void onCircleClick() {
					transformerlKey.press();
				}
				public void onDownClick() {
					downKey.press();
				}
				public void onLeftClick() {
					goLeftKey.press();
				}
				public void onRightClick() {
					goRightKey.press();
				}
				public void onUpClick() {
					upKey.press();
				}
				public void onSquareClick() {}
				
				public void onTriangleClick() {
					jumpKey.press();
				}
	
				public void unCancelClick() {
					cancelKey.release();
				}
				public void unCircleClick() {
					transformerlKey.release();
				}
				public void unDownClick() {
					downKey.release();
				}
				public void unLeftClick() {
					goLeftKey.release();
				}
				public void unRightClick() {
					goRightKey.release();
				}
				public void unSquareClick() {}
				public void unTriangleClick() {
					jumpKey.release();
				}
				public void unUpClick() {
					upKey.release();
				}
			};
			setEmulatorListener(emulatorListener);
			EmulatorButtons ebs = getEmulatorButtons();
			if (ebs != null) {
				
	//				ebs.getUp().disable(true);
	//				ebs.getDown().disable(true);
	//				ebs.getSquare().disable(true);
	//				ebs.getRight().setX(ebs.getRight().getX()-20);
	//				ebs.getRight().setY(ebs.getRight().getY()+1);
	//				ebs.getTriangle().setY(ebs.getTriangle().getY() + 125);
	//				ebs.getCircle().setY(ebs.getSquare().getY() + 110);
	//				ebs.getCancel().setY(ebs.getCancel().getY() + 90);
				
			}
		}
	}
	private void setKeys(){
		
		goLeftKey = new ActionKey();
		goRightKey = new ActionKey();
		jumpKey = new ActionKey();
		upKey= new ActionKey();
		downKey= new ActionKey();
		cancelKey= new ActionKey();
		transformerlKey= new ActionKey();
		
	}
	private void PressedKey(){
		if (!gamestart){
			return;
		}
		if (goLeftKey.isPressed()) {
			// 向左移动
			magic.left();
		} else if (goRightKey.isPressed()) {
			// 向右移动
			magic.right();
		} else {
			// 暂停
			magic.stop();
		}
		if (jumpKey.isPressed()) {
			// 跳跃
			magic.jump();
		}
		if("butterfly".equals(magic.ct.Actortype)){
			//向上
			if (upKey.isPressed()) {
				magic.up();
			}else if (downKey.isPressed()) {//向下
				magic.down();
			}else{
				magic.stopfly();
			}
		}
		if (transformerlKey.isPressed()){
			magic.transform();
		}
		if (cancelKey.isPressed()){
			magic.canceltrans();
		}
		//失败
		if (magic.isfail()){
			magic.rebegin();
		}
	}
	
	
}
